package com.durian.base.net.request;

import androidx.lifecycle.Lifecycle;
import com.durian.base.net.HttpResponse;
import com.durian.base.net.Utils;

import okhttp3.Request;


/**
 * 说明：HttpGetTask
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019/1/30 18:06
 * <p/>
 * 版本：version 1.0
 */
public class OkDelete extends OkHttpTask {

    private OkDelete(Lifecycle lifecycle, String url) {
        super(lifecycle, url);
    }

    public static OkDelete create(Lifecycle lifecycle, String url){
        return new OkDelete(lifecycle, url);
    }

    @Override
    public Request.Builder createRequestBuilder() {
        return new Request.Builder()
                .tag(getUrl())
                .headers(getRequestParams().getHeaders())
                .delete()
                .url(Utils.getFullUrl(getUrl(), getRequestParams().getParams(), getRequestParams().isUrlEncoder()));
    }

    @Override
    public void onStartTask() {
        if (getOnLoadListener() != null){
            getOnLoadListener().onStart();
        }
    }

    @Override
    protected void doCancelTask() {
        super.doCancelTask();
        if (getOnLoadListener() != null){
            getOnLoadListener().onCancel();
        }
    }

    @Override
    public void onSuccessTask(HttpResponse response) {
        if (getOnLoadListener() != null){
            getOnLoadListener().onSuccess(response,response.getResult());
        }
    }

    @Override
    public void onErrorTask(int errorCode, Exception e) {
        if (getOnLoadListener() != null){
            getOnLoadListener().onError(errorCode,e.getMessage());
        }
    }
}
